package com.flyee.viewer
{
	import com.flyee.controller.SkillCDController;
	import com.flyee.utils.BrushPencil;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class SkillButtonViewer extends ImageButtonViewer implements Destroy
	{
		/**
		 * 技能CD控制器 
		 */		
		private var _skillCDController:SkillCDController = null;
		
		/**
		 * 技能ID 
		 */
		public var skillId : int;
		
		/**
		 * 旋转的范围 
		 */		
		private const arc:Number = 360;
		/**
		 * 颜色 
		 */		
		private const rotsColor:uint = 0x6E6E6E;
		/**
		 * alph 
		 */		
		private const alp:Number = 0.7;
		/**
		 * 开始的角度 
		 */		
		private const startingAngle:Number = -90;
		/**
		 *  CD时间
		 */		
		private var _duration:int = 1000;
		/**
		 * 开始时间 
		 */		
		private var startTime:Number;
		/**
		 * 
		 */		
		private var n:Number;
		/**
		 * 
		 */		
		private var rots:Shape;
		/**
		 * 画笔 
		 */		
		private var pencil:BrushPencil;
		/**
		 * 
		 */		
		private var arcX:Number;
		private var arcY:Number;
		/**
		 * 半径 
		 */		
		private var radius:Number;
		/**
		 * 是否结束 
		 */		
		private var finshed:Boolean = true;
		/**
		 * 显示道具数量的标签
		 */
		private var countLab:TextField=null;
		private var _count:int=0;
		
		public function SkillButtonViewer()
		{
			skillCDController = new SkillCDController();
			rots = new Shape();
			pencil = new BrushPencil(rots.graphics);
			this.addChild(rots);
			countLab=createLab();
			initListener();
		}
		
		public function initListener() : void {
			//this.addEventListener(MouseEvent.CLICK,mouseClickHandle);
		}
		
		override public function dispose():void{
			super.dispose();
			skillCDController = null;
		}
		
		public function clickMouse():void{
			if(_skillCDController && _skillCDController.duration){
				_skillCDController.radiate();
			}
			else{
				addEvent();
			}
		}
		
		/**
		 * 设置延迟时间
		 */
		public function set duration(_duration:int):void{
			if(finshed){
				this._duration = _duration;
			}
		}
		
		public function set skillCDController(cdController:SkillCDController):void{
			this._skillCDController = cdController;
		}
		
		public function set count(c:int):void{
			_count=c;
			if (_count > 1)
			{
				countLab.text=c.toString();
				if (!this.contains(countLab))
				{
					countLab.x=40 - countLab.width;
					countLab.y=40 - countLab.height;
					this.addChild(countLab);
				}
			}
			else
			{
				countLab.text="";
			}
		}
		
		public function get count():int{
			return _count;
		}
		
		public function addEvent():void{
			if(finshed){
				finshed = false;
				startTime = new Date().getTime();
				arcX = this.width*0.5;
				arcY = this.height*0.5;
				radius = Math.sqrt(Math.pow(this.width,2)+Math.pow(this.height,2))*0.5;
				if(_duration > 0)
					this.addEventListener(Event.ENTER_FRAME,frameHandle);
			}
		}
		/**
		 * 创建一个标签，该标签不具有任何交互事件
		 * @return
		 *
		 */
		private function createLab():TextField
		{
			var txt:TextField=new TextField();
			var format:TextFormat=new TextFormat("Birch Std", 15, 0xffffff, null);
			txt.autoSize=TextFieldAutoSize.CENTER;
			txt.selectable=false;
			txt.mouseEnabled=true;
			txt.mouseWheelEnabled=false;
			/* txt.background = true;
			 txt.backgroundColor = 0x000000; */
			txt.thickness=14;
			//txt.border = true;
			txt.defaultTextFormat=format;
			//ColorMagicOfText.bewrite(txt);
			return txt;
		}
		
		private function mouseClickHandle(e:MouseEvent):void{
			clickMouse();
		}
		
		private function frameHandle(e:Event):void{
			n = arc/_duration*(new Date().getTime()-startTime);
			pencil.clear();
			pencil.beginFill(rotsColor,alp);
			pencil.lineStyle(0,0,0);
			pencil.drawArc(arcX,arcY,radius,arc-n,n+startingAngle,true);
			pencil.endFill();
			pencil.beginFill(rotsColor,0);
			pencil.lineStyle(0,0,0);
			pencil.drawArc(arcX,arcY,radius,n,startingAngle,true);
			pencil.endFill();
			if(n>arc){
				n = 0;
				finshed = true;
				pencil.clear();
				this.removeEventListener(Event.ENTER_FRAME,frameHandle);
			}
		}

	}
}